import tkinter as tk
from BankDatabase import BankDatabase
from bank01.Simulate import BankSimulator


class BankApp:
    def __init__(self, db):
        self.db = db
        self.root = tk.Tk()
        self.root.title("银行模拟系统界面")
        self.create_login_widgets()
        self.root.mainloop()

    def create_login_widgets(self):
        tk.Label(self.root, text='银行卡账号：').grid(row=0, column=0)
        tk.Label(self.root, text='密码：').grid(row=1, column=0)
        self.label_msg = tk.Label(self.root, text='')
        self.label_msg.grid(row=2, column=1)

        self.entry_card_id = tk.Entry(self.root)
        self.entry_card_id.grid(row=0, column=1, padx=10, pady=5)
        self.entry_password = tk.Entry(self.root, show='*')
        self.entry_password.grid(row=1, column=1, padx=10, pady=5)

        tk.Button(self.root, text='登录', command=self.login).grid(row=3, column=0, sticky=tk.W, padx=30, pady=5)
        tk.Button(self.root, text='注册', command=self.open_register).grid(row=3, column=1, sticky=tk.E, padx=30,
                                                                           pady=5)

    def login(self):
        card_id = self.entry_card_id.get()
        password = self.entry_password.get()
        card = self.db.query_bank_cards(card_id, password)
        print(card)
        if card is None:
            self.label_msg.config(text="账号或密码错误")
        else:
            self.label_msg.config(text="登录成功")
            bank_simulator = BankSimulator()
            bank_simulator.root.mainloop()

    def open_register(self):
        self.root.withdraw()
        RegisterWindow(self)

    def return_to_root(self, window):
        window.destroy()
        self.root.deiconify()

class RegisterWindow:
    def __init__(self, app):
        self.app = app
        self.db = app.db
        self.window = tk.Toplevel(app.root)
        self.window.title("注册")
        self.create_register_widgets()

    def create_register_widgets(self):
        tk.Label(self.window, text='银行卡账号：').grid(row=0, column=0)
        tk.Label(self.window, text='密码：').grid(row=1, column=0)
        self.label_msg = tk.Label(self.window, text="")
        self.label_msg.grid(row=2, column=1, padx=10, pady=5)

        self.entry_card_id = tk.Entry(self.window)
        self.entry_card_id.grid(row=0, column=1, padx=10, pady=5)
        self.entry_password = tk.Entry(self.window, show='*')
        self.entry_password.grid(row=1, column=1, padx=10, pady=5)

        tk.Button(self.window, text="注册", command=self.register).grid(row=3, column=0, sticky=tk.W, padx=30, pady=5)
        tk.Button(self.window, text="返回登录", command=lambda: self.app.return_to_root(self.window)).grid(row=3,column=1,sticky=tk.W, padx=30, pady=5)

    def register(self):
        card_id = self.entry_card_id.get()
        password = self.entry_password.get()
        rst = self.db.add_card(card_id, password)
        # Add registration logic here, e.g., self.app.db.create_bank_card(card_id, password)
        if rst:
            self.label_msg.config(text="注册成功")
        else:
            self.label_msg.config(text="注册失败，账号已存在")


